const express = require("express");
const router = express.Router();

const { timeTrans } = require("../../../utils");
const { getLogin } = require("../../../model/weixin/admin/login");

// 使用token
const { sign, verify } = require("../../../utils/token");

/* banner增删改查 */

// 查找users数据
router.post("/", async function (req, res, next) {
  const { username, password } = req.body;

  try {
    let data = await getLogin(username);
    console.log("data", data);
    if (data[0].username === username) {
      if (data[0].password === password) {
        // 生成token
        const token = sign(username);
        // 返回成功信息
        res.send({
          code: 200,
          msg: "登录成功",
          username: data[0].username,
          nickname: data[0].nickname,
          token,
        });
      } else {
        res.send({
          code: 401,
          msg: "登录密码输入错误，请重新输入！",
        });
      }
    } else {
      res.send({
        code: 402,
        msg: "登录用户名输入错误，请重新输入！",
      });
    }
  } catch (error) {
    res.send({
      code: 402,
      msg: "登录用户名输入错误，请重新输入！",
      err: error.message,
    });
  }
});

// 暴露路由
module.exports = router;
